November 1, 2017

viridis package

From the package's GitHub repository:

"These four color maps are designed in such a way that they will analytically be perfectly perceptually-uniform, both in regular form and also when converted to black-and-white. They are also designed to be perceived by readers with the most common form of color blindness."

  • Viridis is now the default color map for Matplotlib, a key Python plotting library.
  • Several of the packages we'll look at today use Viridis as the default color map.
  • Upcoming versions of ggplot2 will include this color palette

viridis package

Here is an example (from the helpfile) of a hexbin graph of random values that uses the Viridis color map:

library(viridis); library(hexbin)
dat <- data.frame(x = rnorm(10000), y = rnorm(10000))
ggplot(dat, aes(x = x, y = y)) + geom_hex() + coord_fixed() +
  scale_fill_viridis()

viridis package

The option argument allows you to pick between four color maps: Magma, Inferno, Plasma, and Viridis. Here is the code to visualize each of those (plot on next page):

library(gridExtra)
ex_plot <- ggplot(dat, aes(x = x, y = y)) + geom_hex() + coord_fixed()
magma_plot <- ex_plot + scale_fill_viridis(option = "A")
inferno_plot <- ex_plot + scale_fill_viridis(option = "B")
plasma_plot <- ex_plot + scale_fill_viridis(option = "C")
viridis_plot <- ex_plot + scale_fill_viridis()
grid.arrange(magma_plot, inferno_plot, plasma_plot, viridis_plot, ncol = 2)

viridis package

htmlWidgets

Very smart people have been working on creating interactive graphics in R for a long time. So far, nothing "native" to" R has taken off in a big way (although keep an eye out for ggvis).

There is now a series of R packages that allow you to create plots from these JavaScript libraries from within R.

There is a website with much more on these htmlWidgets at http://www.htmlwidgets.org.

htmlWidgets

JavaScript has developed a number of interactive graphics libraries that can be for documents viewed in a web browser. These work by binding data to support vector graphics (SVGs).

They allow you to do things like zoom and pan. The graphics can also "react" to certain events. For example, they can show a pop-up when you hover over or click on a point.

htmlWidgets

Some of the packages availabe to help you create interactive graphics from R using JavaScript graphics libraries:

  • leaflet: Mapping (we'll cover this next week)
  • dygraphs: Time series
  • plotly: A variety of plots, including maps
  • rbokeh: A variety of plots, including maps
  • networkD3: Network data
  • d3heatmap: Heatmaps
  • DT: Data tables
  • DiagrammeR: Diagrams and flowcharts

htmlWidgets

These packages can be used to make some pretty cool interactive visualizations for HTML output from R Markdown or Shiny (you can also render any of theme in RStudio).

There are, however, a few limitations:

  • Written by different people. The different packages have different styles as well as different interfaces. Learning how to use one package may not help you much with other of these packages.
  • Many are still in development, often in early development.

plotly package

From the package documentation:

"Easily translate ggplot2 graphs to an interactive web-based version and / or create custom web-based visualizations directly from R."

  • Like many of the packages today, draws on functionality external to R, but within a package that allows you to work exclusively within R.
  • Allows you to create interactive graphs from R. The functions extend much of the ggplot2 code you've learned.
  • Interactivity will only work within RStudio or on documents rendered to HTML.

plotly package

The plotly package allows an interface to let you work with plotly.js code directly using R code.

plotly.js is an open source library for creating interactive graphs in JavaScript. This JavaScript library is built on d3.js (Data-Driven Documents), which is a key driver in interactive web-based data graphics today.

plotly package

There are two main ways of create plots within plotly:

  • Use one of the functions to create a customized interactive graphic:
    • plot_ly: Workhorse of plotly, renders most non-map types of graphs
    • plot_geo, plot_mapbax: Specific functions for creating plotly maps.
  • Create a ggplot object and then convert it to a plotly object using the ggplotly function.

plotly package

library(faraway); data(worldcup); library(dplyr)
library(plotly)
a <- worldcup %>% ggplot(aes(x = Time, y = Shots)) + geom_point()
ggplotly(a)

plotly package

a <- worldcup %>% ggplot(aes(x = Time, y = Shots, color = Position)) + 
  geom_point()
ggplotly(a)

plotly package

You can also use this with other ggplot2 functionality, like faceting:

shots_vs_time <- worldcup %>%
  mutate(Name = rownames(worldcup)) %>%
  filter(Team %in% c("Netherlands", "Germany", "Spain", "Uruguay")) %>%
  ggplot(aes(x = Time, y = Shots, color = Position, group = Name)) + 
  geom_point() + 
  facet_wrap(~ Team)
ggplotly(shots_vs_time)

plotly package

plotly package

If you pipe to the rangeslider function, it allows the viewer to zoom in on part of the x range. (This can be particularly nice for time series.)

You should have a dataset available through your R session named USAccDeaths. This gives a montly county of accidental deaths in the US for 1973 to 1978. This code will plot it and add a range slider on the lower x-axis.

plot_ly(x = time(USAccDeaths), y = USAccDeaths) %>% 
  add_lines() %>% rangeslider()

plotly package

plotly package

For a 3-D scatterplot, add a mapping to the z variable:

worldcup %>%
  plot_ly(x = ~ Time, y = ~ Shots, z = ~ Passes,
          color = ~ Position, size = I(4)) %>%
  add_markers()

plotly package

plotly package

The volcano data comes with R and is in a matrix format. Each value gives the elevation for a particular pair of x- and y-coordinates.

dim(volcano)
## [1] 87 61
volcano[1:4, 1:4]
##      [,1] [,2] [,3] [,4]
## [1,]  100  100  101  101
## [2,]  101  101  102  102
## [3,]  102  102  103  103
## [4,]  103  103  104  104

plotly package

plot_ly(z = ~ volcano, type = "surface")

plotly package

Mapping with plotly can build on some data that comes with base R or other packages you've likely added (or can add easily, as with the map_data function from ggplot2). For example, we can map state capitals and cities with > 40,000 people using data in the us.cities dataframe in the maps package:

head(maps::us.cities, 3)
##         name country.etc    pop   lat    long capital
## 1 Abilene TX          TX 113888 32.45  -99.74       0
## 2   Akron OH          OH 206634 41.08  -81.52       0
## 3 Alameda CA          CA  70069 37.77 -122.26       0

plotly package

Here is code you can use to map all of these cities on a US map:

ggplot2::map_data("state") %>%
   group_by(group) %>% 
   plot_ly(x = ~long, y = ~lat) %>%
   add_polygons(hoverinfo = "none") %>%
   add_markers(text = ~paste(name, "<br />", pop), hoverinfo = "text",
               alpha = 0.25,
     data = filter(maps::us.cities, -125 < long & long < -60 &
                     25 < lat & lat < 52)) %>%
   layout(showlegend = FALSE)

plotly package

plotly package

The creator of the R plotly package has written a bookdown book on the package that you can read here. It provides extensive details and examples for using plotly.

Getting Started with D3 by Mike Dewar (a short book on D3 in JavaScript) is available for free here.

rbokeh package

rbokeh package

library(rbokeh)
figure(width = 600, height = 300) %>%
  ly_points(Time, Shots, data = worldcup,
    color = Position, hover = list(Time, Shots))

rbokeh package

This package can also be used to create interactive maps. For example, the following dataset has data on Oregon climate stations, including locations:

orstationc <- read.csv(paste0("http://geog.uoregon.edu/bartlein/",
                              "old_courses/geog414s05/data/orstationc.csv"))
head(orstationc, 3)
##   station    lat      lon elev tjan tjul tann pjan pjul pann  idnum
## 1     ANT 44.917 -120.717  846  0.0 20.2  9.6   41    9  322 350197
## 2     ARL 45.717 -120.200   96  0.9 24.6 12.5   40    6  228 350265
## 3     ASH 42.217 -122.717  543  3.1 20.8 11.1   70    7  480 350304
##                  Name
## 1 ANTELOPE 1 N USA-OR
## 2    ARLINGTON USA-OR
## 3  ASHLAND 1 N USA-OR

rbokeh package

You can use the following code to create an interactive map of these climate stations:

gmap(lat = 44.1, lng = -120.767, zoom = 6,
     width = 700, height = 600) %>%
  ly_points(lon, lat, data = orstationc, alpha = 0.8, col = "red",
    hover = c(station, Name, elev, tann))

rbokeh package

rbokeh package

dygraphs package

The dygraphs package lets you create interactive time series plots from R using the dygraphs JavaScript library.

The main function syntax is fairly straightforward. Like many of these packages, it allows piping.

There is a website with more information on using dygraphs available at http://rstudio.github.io/dygraphs/index.html.

dygraphs package

For example, here is the code to plot monthly deaths from lung diseases in the UK in the 1970s.

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths) %>%
  dySeries("mdeaths", label = "Male") %>%
  dySeries("fdeaths", label = "Female")

dygraphs package

For example, here is the code to plot monthly deaths from lung diseases in the UK in the 1970s.

DT package

The DT package provides a way to create interactive tables in R using the JavaScript DataTables library.

We've already seen some examples of this output in some of the Shiny apps I showed last week. You can also use this package to include interactive tables in R Markdown documents you plan to render to HTML.

There is a website with more information on this package at http://rstudio.github.io/DT/.

DT package

library(DT)
datatable(worldcup)

d3heatmap package

The d3heatmap package allows you to create interactive heatmaps. For example:

library(d3heatmap)
d3heatmap(mtcars, scale = "column")

d3heatmap package

networkD3 package

The networkd3 package allows you to create different networks. For example, a simple network:

library(networkD3)
src <- c("A", "A", "A", "A",
        "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
            "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
simpleNetwork(networkData)

networkD3 package

networkD3 package

A more complex network:

data(MisLinks)
data(MisNodes)
forceNetwork(Links = MisLinks, Nodes = MisNodes,
            Source = "source", Target = "target",
            Value = "value", NodeID = "name",
            Group = "group", opacity = 0.8)

networkD3 package

networkD3 package

A Sankey diagram:

URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata/energy.json")
Energy <- jsonlite::fromJSON(URL)
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, 
              Source = "source", Target = "target", 
              Value = "value", NodeID = "name",
              units = "TWh", fontSize = 12, 
              nodeWidth = 30)

networkD3 package

networkD3 package

A diagonal network:

URL <- paste0(
        "https://cdn.rawgit.com/christophergandrud/networkD3/",
        "master/JSONdata//flare.json")
Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE)
Flare$children = Flare$children[1:3]
diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9)

networkD3 package

networkD3 package

Creating your own widget

If you find a JavaScript visualization library and would like to create bindings to R, you can create your own package for a new htmlWidget.

There is advice on creating your own widget for R available at http://www.htmlwidgets.org/develop_intro.html.